package com.skydroid.rcsdkdemo;

import android.util.Log;

import com.skydroid.rcsdk.PayloadManager;
import com.skydroid.rcsdk.comm.CommListener;
import com.skydroid.rcsdk.common.error.SkyException;
import com.skydroid.rcsdk.common.payload.C10Pro;
import com.skydroid.rcsdk.common.payload.PayloadType;

import java.nio.charset.StandardCharsets;

/**
 * UDP消息发送工具类
 * 用于向指定服务器发送UDP消息并接收响应
 */
public class UDPMessageSender {
    private static final String TAG = "UDPMessageSender";
    private C10Pro udpPayload;
    private String serverAddress;
    private int serverPort;
    private boolean isConnected = false;
    private OnMessageReceivedListener listener;

    /**
     * 构造函数
     * @param serverAddress 服务器地址
     * @param serverPort 服务器端口
     */
    public UDPMessageSender(String serverAddress, int serverPort) {
        this.serverAddress = serverAddress;
        this.serverPort = serverPort;
        init();
    }

    /**
     * 初始化UDP连接
     */
    private void init() {
        // 创建UDP负载，使用C10Pro类型作为UDP通信的基础
        udpPayload = (C10Pro) PayloadManager.INSTANCE.getUDPPayload(
                PayloadType.C10PRO, 
                0, // 本地端口使用0表示随机分配
                serverAddress, 
                serverPort
        );
        
        if (udpPayload != null) {
            // 设置通信监听器
            udpPayload.setCommListener(new CommListener() {
                @Override
                public void onConnectSuccess() {
                    Log.d(TAG, "UDP连接成功: " + serverAddress + ":" + serverPort);
                    isConnected = true;
                }

                @Override
                public void onConnectFail(SkyException e) {
                    Log.e(TAG, "UDP连接失败: " + e.getMessage());
                    isConnected = false;
                }

                @Override
                public void onDisconnect() {
                    Log.d(TAG, "UDP连接断开");
                    isConnected = false;
                }

                @Override
                public void onReadData(byte[] bytes) {
                    String response = new String(bytes, StandardCharsets.UTF_8);
                    Log.d(TAG, "收到响应: " + response);
                    if (listener != null) {
                        listener.onMessageReceived(response);
                    }
                }
            });
            
            // 连接到UDP目标
            PayloadManager.INSTANCE.connectPayload(udpPayload);
        } else {
            Log.e(TAG, "创建UDP负载失败");
        }
    }

    /**
     * 发送消息
     * @param message 要发送的消息
     * @return 是否发送成功
     */
    public boolean sendMessage(String message) {
        if (udpPayload != null && isConnected) {
            try {
                byte[] data = message.getBytes(StandardCharsets.UTF_8);
                udpPayload.writeData(data);
                Log.d(TAG, "已发送消息: " + message);
                return true;
            } catch (Exception e) {
                Log.e(TAG, "发送消息失败: " + e.getMessage());
                return false;
            }
        } else {
            Log.e(TAG, "UDP未连接，无法发送消息");
            return false;
        }
    }

    /**
     * 关闭连接
     */
    public void close() {
        if (udpPayload != null) {
            PayloadManager.INSTANCE.disconnectPayload(udpPayload);
            udpPayload = null;
            isConnected = false;
        }
    }

    /**
     * 设置消息接收监听器
     * @param listener 监听器
     */
    public void setOnMessageReceivedListener(OnMessageReceivedListener listener) {
        this.listener = listener;
    }

    /**
     * 获取服务器地址
     * @return 服务器地址
     */
    public String getServerAddress() {
        return serverAddress;
    }

    /**
     * 获取服务器端口
     * @return 服务器端口
     */
    public int getServerPort() {
        return serverPort;
    }

    /**
     * 消息接收监听器接口
     */
    public interface OnMessageReceivedListener {
        void onMessageReceived(String message);
    }
} 